<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/23
 * Time: 18:02
 */
namespace app\api\model;

use app\api\model\Category as CategoryModel;

class Homeuser extends Base
{
    protected $hidden = ['id','password','adddate','openid_weixin','login_ip','editdate','openid_xcx_weixin'];
    protected $json = ['params'];
    protected $jsonAssoc = true;                            //json->array
    protected $autoWriteTimestamp = 'datetime';                   //自动时间戳转换
    protected $field = true;
//    protected $type = [
//        'params'  =>  'json',
//    ];
    protected $createTime = 'adddate';
//    protected $updateTime = 'update_at';
    protected $insert = ['complete' => 1];
    protected $auto = ['login_ip'];

    //性别转换
    public function getsexAttr($value)
    {
        $sex = config('setting.sex');
        return $sex[$value];
    }

    //院系转换
    public function getcollegeAttr($value){
        return categoryModel::where(['id'=>$value,'publish'=>'1'])->field('id,title')->find();
    }

    //专业转换
    public function getclassAttr($value){
        return categoryModel::where(['id'=>$value,'publish'=>'1'])->field('id,title')->find();
    }

    //图片路径转换
    public function getimageAttr($value){
        $http1 = 'http://139.199.60.80:8080/';  //api端
        $http2 = 'http://srms.telecomlab.cn/ZZX/cxcyxy2/';
        if(@fopen ($http2.$value, 'r')){
            return $http2.$value;
        }else if(@fopen ($http1.$value, 'r')){
            return $http1.$value;
        }
    }

    //图片路径转换
    public function getimageThumbAttr($value){
        $http1 = 'http://139.199.60.80:8080/';  //api端
        $http2 = 'http://srms.telecomlab.cn/ZZX/cxcyxy2/';
        if(@fopen ($http2.$value, 'r')){
            return $http2.$value;
        }else if(@fopen ($http1.$value, 'r')){
            return $http1.$value;
        }
    }

    public function setpasswordAttr($value)
    {
        return MD5(MD5($value));
    }

    protected function setloginIpAttr()
    {
        return request()->ip();
    }

    protected function setparamsAttr($value)
    {
        return json_encode($value);
    }

    //获取指定用户openid
    public static function getByOpenID($openid,$id)
    {
        $openid_xcx_weixin = self::where(['id'=>$id])->value('openid_weixin');
        if(empty($openid_xcx_weixin)) {
            self::where(['id'=>$id])->update(['openid_weixin'=>$openid]);//没有进行授权
            $result = $id;
        }else if(!empty($openid_xcx_weixin) && $openid_xcx_weixin == $openid) {
            $result = $id;
        }else {
            $result = 0;
        }
        return $result;
    }
}